<?
    /* Last updated with phpFlickr 1.3.1
     *
     * Edit these variables to reflect the values you need. $default_redirect
     * and $permissions are only important if you are linking here instead of
     * using phpFlickr::auth() from another page or if you set the remember_uri
     * argument to false.
     */
    $api_key                 = "348d57b327a3184f2a00166abf980d4a";
    $api_secret              = "1102c4c6c55bc433";
    $default_redirect        = "success.php";
    $permissions             = "write";
    $path_to_phpFlickr_class = "classes/phpFlickr-2.3.0.1/";

    ob_start();
    require_once($path_to_phpFlickr_class . "phpFlickr.php");
    require_once(dirname(__FILE__).'/../db/functions.php');

    unset($_SESSION['phpFlickr_auth_token']);

	if (!empty($_GET['extra'])) {
		$redirect = $_GET['extra'];
	}

    $f = new phpFlickr($api_key, $api_secret);

    if (empty($_GET['frob'])) 
    {
        $f->auth($permissions, false);
    } 
    else 
    {
      $response = $f->auth_getToken($_GET['frob']);
      $token = $response['token'];
      
      //response contains flickr username - search for this in db and fill in the gaps we've just learnt      
      $user_res = db_query("SELECT id FROM flickr WHERE flickr_username = '".$response['username']."';");
      $user_arr = mysql_fetch_array($user_res);
      $id = $user_arr[0];

      db_query("UPDATE flickr SET flickr_id = '".$response['nsid']."', flickr_auth_string = '".$token."' WHERE id = ".$id." LIMIT 1;");
	  }

    if (empty($redirect)) {
		header("Location: " . $default_redirect);
    } else {
		header("Location: " . $redirect);
    }

?>
